home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / rdflib / store / SQLite.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  14.2 KB  |  421 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import generators
  5. from rdflib import BNode
  6. from rdflib.Literal import Literal
  7. from pprint import pprint
  8. from pysqlite2 import dbapi2
  9. import sha
  10. import sys
  11. import re
  12. import os
  13. from rdflib.term_utils import *
  14. from rdflib.Graph import QuotedGraph
  15. from rdflib.store.REGEXMatching import REGEXTerm, NATIVE_REGEX, PYTHON_REGEX
  16. from rdflib.store.AbstractSQLStore import *
  17. Any = None
  18.  
  19. def regexp(expr, item):
  20.     r = re.compile(expr)
  21.     return r.match(item) is not None
  22.  
  23.  
  24. class SQLite(AbstractSQLStore):
  25.     """
  26.     SQLite store formula-aware implementation.  It stores it's triples in the following partitions:
  27.  
  28.     - Asserted non rdf:type statements
  29.     - Asserted rdf:type statements (in a table which models Class membership)
  30.     The motivation for this partition is primarily query speed and scalability as most graphs will always have more rdf:type statements than others
  31.     - All Quoted statements
  32.  
  33.     In addition it persists namespace mappings in a seperate table
  34.     """
  35.     context_aware = True
  36.     formula_aware = True
  37.     transaction_aware = True
  38.     regex_matching = PYTHON_REGEX
  39.     autocommit_default = False
  40.     
  41.     def open(self, home, create = True):
  42.         '''
  43.         Opens the store specified by the configuration string. If
  44.         create is True a store will be created if it does not already
  45.         exist. If create is False and a store does not already exist
  46.         an exception is raised. An exception is also raised if a store
  47.         exists, but there is insufficient permissions to open the
  48.         store.'''
  49.         if create:
  50.             db = dbapi2.connect(os.path.join(home, self.identifier))
  51.             c = db.cursor()
  52.             c.execute(CREATE_ASSERTED_STATEMENTS_TABLE % self._internedId)
  53.             c.execute(CREATE_ASSERTED_TYPE_STATEMENTS_TABLE % self._internedId)
  54.             c.execute(CREATE_QUOTED_STATEMENTS_TABLE % self._internedId)
  55.             c.execute(CREATE_NS_BINDS_TABLE % self._internedId)
  56.             c.execute(CREATE_LITERAL_STATEMENTS_TABLE % self._internedId)
  57.             for tblName, indices in [
  58.                 ('%s_asserted_statements', [
  59.                     ('%s_A_termComb_index', ('termComb',)),
  60.                     ('%s_A_s_index', ('subject',)),
  61.                     ('%s_A_p_index', ('predicate',)),
  62.                     ('%s_A_o_index', ('object',)),
  63.                     ('%s_A_c_index', ('context',))]),
  64.                 ('%s_type_statements', [
  65.                     ('%s_T_termComb_index', ('termComb',)),
  66.                     ('%s_member_index', ('member',)),
  67.                     ('%s_klass_index', ('klass',)),
  68.                     ('%s_c_index', ('context',))]),
  69.                 ('%s_literal_statements', [
  70.                     ('%s_L_termComb_index', ('termComb',)),
  71.                     ('%s_L_s_index', ('subject',)),
  72.                     ('%s_L_p_index', ('predicate',)),
  73.                     ('%s_L_c_index', ('context',))]),
  74.                 ('%s_quoted_statements', [
  75.                     ('%s_Q_termComb_index', ('termComb',)),
  76.                     ('%s_Q_s_index', ('subject',)),
  77.                     ('%s_Q_p_index', ('predicate',)),
  78.                     ('%s_Q_o_index', ('object',)),
  79.                     ('%s_Q_c_index', ('context',))]),
  80.                 ('%s_namespace_binds', [
  81.                     ('%s_uri_index', ('uri',))])]:
  82.                 for indexName, columns in indices:
  83.                     c.execute('CREATE INDEX %s on %s (%s)' % (indexName % self._internedId, tblName % self._internedId, ','.join(columns)))
  84.                 
  85.             
  86.             c.close()
  87.             db.commit()
  88.             db.close()
  89.         
  90.         self._db = dbapi2.connect(os.path.join(home, self.identifier))
  91.         self._db.create_function('regexp', 2, regexp)
  92.         if os.path.exists(os.path.join(home, self.identifier)):
  93.             c = self._db.cursor()
  94.             c.execute("SELECT * FROM sqlite_master WHERE type='table'")
  95.             tbls = [ rt[1] for rt in c.fetchall() ]
  96.             c.close()
  97.             for tbl in table_name_prefixes:
  98.                 pass
  99.             
  100.             return 1
  101.  
  102.     
  103.     def destroy(self, home):
  104.         '''
  105.         FIXME: Add documentation
  106.         '''
  107.         db = dbapi2.connect(os.path.join(home, self.identifier))
  108.         c = db.cursor()
  109.         for tblsuffix in table_name_prefixes:
  110.             
  111.             try:
  112.                 c.execute('DROP table %s' % tblsuffix % self._internedId)
  113.             continue
  114.             print 'unable to drop table: %s' % tblsuffix % self._internedId
  115.             continue
  116.  
  117.         
  118.         print 'Destroyed Close World Universe %s ( in SQLite database %s)' % (self.identifier, home)
  119.         db.commit()
  120.         c.close()
  121.         db.close()
  122.         os.remove(os.path.join(home, self.identifier))
  123.  
  124.     
  125.     def EscapeQuotes(self, qstr):
  126.         '''
  127.         Ported from Ft.Lib.DbUtil
  128.         '''
  129.         if qstr is None:
  130.             return ''
  131.         tmp = qstr.replace('\\', '\\\\')
  132.         tmp = tmp.replace('"', '""')
  133.         tmp = tmp.replace("'", "\\'")
  134.         return tmp
  135.  
  136.     
  137.     def normalizeTerm(self, term):
  138.         if isinstance(term, (QuotedGraph, Graph)):
  139.             return term.identifier
  140.         if isinstance(term, Literal):
  141.             return self.EscapeQuotes(term)
  142.         if term is None or isinstance(term, (list, REGEXTerm)):
  143.             return term
  144.         return term
  145.  
  146.     
  147.     def buildSubjClause(self, subject, tableName):
  148.         if isinstance(subject, REGEXTerm):
  149.             if not tableName or '%s.subject' % tableName:
  150.                 pass
  151.             return (' REGEXP (%s,' + ' %s)' % 'subject', [
  152.                 subject])
  153.         if isinstance(subject, list):
  154.             clauseStrings = []
  155.             paramStrings = []
  156.             for s in subject:
  157.                 if isinstance(s, REGEXTerm):
  158.                     if not tableName or '%s.subject' % tableName:
  159.                         pass
  160.                     clauseStrings.append(' REGEXP (%s,' + ' %s)' % 'subject' + ' %s')
  161.                     paramStrings.append(self.normalizeTerm(s))
  162.                     continue
  163.                 isinstance(subject, REGEXTerm)
  164.                 if isinstance(s, (QuotedGraph, Graph)):
  165.                     if not tableName or '%s.subject' % tableName:
  166.                         pass
  167.                     clauseStrings.append('%s=' % 'subject' + '%s')
  168.                     paramStrings.append(self.normalizeTerm(s.identifier))
  169.                     continue
  170.                 if not tableName or '%s.subject' % tableName:
  171.                     pass
  172.                 clauseStrings.append('%s=' % 'subject' + '%s')
  173.                 paramStrings.append(self.normalizeTerm(s))
  174.             
  175.             return ('(' + ' or '.join(clauseStrings) + ')', paramStrings)
  176.         if isinstance(subject, (QuotedGraph, Graph)):
  177.             if not tableName or '%s.subject' % tableName:
  178.                 pass
  179.             return ('%s=' % 'subject' + '%s', [
  180.                 self.normalizeTerm(subject.identifier)])
  181.         if subject is not None:
  182.             if not tableName or '%s.subject' % tableName:
  183.                 pass
  184.         if not [
  185.             subject]:
  186.             pass
  187.         return ('%s=' % 'subject' + '%s', None)
  188.  
  189.     
  190.     def buildPredClause(self, predicate, tableName):
  191.         if isinstance(predicate, REGEXTerm):
  192.             if not tableName or '%s.predicate' % tableName:
  193.                 pass
  194.             return (' REGEXP (%s,' + ' %s)' % 'predicate', [
  195.                 predicate])
  196.         if isinstance(predicate, list):
  197.             clauseStrings = []
  198.             paramStrings = []
  199.             for p in predicate:
  200.                 if isinstance(p, REGEXTerm):
  201.                     if not tableName or '%s.predicate' % tableName:
  202.                         pass
  203.                     clauseStrings.append(' REGEXP (%s,' + ' %s)' % 'predicate')
  204.                 elif not tableName or '%s.predicate' % tableName:
  205.                     pass
  206.                 clauseStrings.append('%s=' % 'predicate' + '%s')
  207.                 paramStrings.append(self.normalizeTerm(p))
  208.             
  209.             return ('(' + ' or '.join(clauseStrings) + ')', paramStrings)
  210.         if predicate is not None:
  211.             if not tableName or '%s.predicate' % tableName:
  212.                 pass
  213.         if not [
  214.             predicate]:
  215.             pass
  216.         return ('%s=' % 'predicate' + '%s', None)
  217.  
  218.     
  219.     def buildObjClause(self, obj, tableName):
  220.         if isinstance(obj, REGEXTerm):
  221.             if not tableName or '%s.object' % tableName:
  222.                 pass
  223.             return (' REGEXP (%s,' + ' %s)' % 'object', [
  224.                 obj])
  225.         if isinstance(obj, list):
  226.             clauseStrings = []
  227.             paramStrings = []
  228.             for o in obj:
  229.                 if isinstance(o, REGEXTerm):
  230.                     if not tableName or '%s.object' % tableName:
  231.                         pass
  232.                     clauseStrings.append(' REGEXP (%s,' + ' %s)' % 'object')
  233.                     paramStrings.append(self.normalizeTerm(o))
  234.                     continue
  235.                 isinstance(obj, REGEXTerm)
  236.                 if isinstance(o, (QuotedGraph, Graph)):
  237.                     if not tableName or '%s.object' % tableName:
  238.                         pass
  239.                     clauseStrings.append('%s=' % 'object' + '%s')
  240.                     paramStrings.append(self.normalizeTerm(o.identifier))
  241.                     continue
  242.                 if not tableName or '%s.object' % tableName:
  243.                     pass
  244.                 clauseStrings.append('%s=' % 'object' + '%s')
  245.                 paramStrings.append(self.normalizeTerm(o))
  246.             
  247.             return ('(' + ' or '.join(clauseStrings) + ')', paramStrings)
  248.         if isinstance(obj, (QuotedGraph, Graph)):
  249.             if not tableName or '%s.object' % tableName:
  250.                 pass
  251.             return ('%s=' % 'object' + '%s', [
  252.                 self.normalizeTerm(obj.identifier)])
  253.         if obj is not None:
  254.             if not tableName or '%s.object' % tableName:
  255.                 pass
  256.         if not [
  257.             obj]:
  258.             pass
  259.         return ('%s=' % 'object' + '%s', None)
  260.  
  261.     
  262.     def buildContextClause(self, context, tableName):
  263.         if not context is not None or self.normalizeTerm(context.identifier):
  264.             pass
  265.         context = context
  266.         if isinstance(context, REGEXTerm):
  267.             if not tableName or '%s.context' % tableName:
  268.                 pass
  269.             return (' REGEXP (%s,' + ' %s)' % 'context', [
  270.                 context])
  271.         if context is not None:
  272.             if not tableName or '%s.context' % tableName:
  273.                 pass
  274.         if not [
  275.             context]:
  276.             pass
  277.         return ('%s=' % 'context' + '%s', None)
  278.  
  279.     
  280.     def buildTypeMemberClause(self, subject, tableName):
  281.         if isinstance(subject, REGEXTerm):
  282.             if not tableName or '%s.member' % tableName:
  283.                 pass
  284.             return (' REGEXP (%s,' + ' %s)' % 'member', [
  285.                 subject])
  286.         if isinstance(subject, list):
  287.             clauseStrings = []
  288.             paramStrings = []
  289.             for s in subject:
  290.                 clauseStrings.append('%s.member=' % tableName + '%s')
  291.                 if isinstance(s, (QuotedGraph, Graph)):
  292.                     paramStrings.append(self.normalizeTerm(s.identifier))
  293.                     continue
  294.                 isinstance(subject, REGEXTerm)
  295.                 paramStrings.append(self.normalizeTerm(s))
  296.             
  297.             return ('(' + ' or '.join(clauseStrings) + ')', paramStrings)
  298.         if subject:
  299.             pass
  300.         return (u'%s.member = ' % tableName + '%s', [
  301.             subject])
  302.  
  303.     
  304.     def buildTypeClassClause(self, obj, tableName):
  305.         if isinstance(obj, REGEXTerm):
  306.             if not tableName or '%s.klass' % tableName:
  307.                 pass
  308.             return (' REGEXP (%s,' + ' %s)' % 'klass', [
  309.                 obj])
  310.         if isinstance(obj, list):
  311.             clauseStrings = []
  312.             paramStrings = []
  313.             for o in obj:
  314.                 clauseStrings.append('%s.klass=' % tableName + '%s')
  315.                 if isinstance(o, (QuotedGraph, Graph)):
  316.                     paramStrings.append(self.normalizeTerm(o.identifier))
  317.                     continue
  318.                 isinstance(obj, REGEXTerm)
  319.                 paramStrings.append(self.normalizeTerm(o))
  320.             
  321.             return ('(' + ' or '.join(clauseStrings) + ')', paramStrings)
  322.         if obj is not None:
  323.             pass
  324.         if not [
  325.             obj]:
  326.             pass
  327.         return ('%s.klass = ' % tableName + '%s', None)
  328.  
  329.     
  330.     def triples(self, .1, context = None):
  331.         '''
  332.         A generator over all the triples matching pattern. Pattern can
  333.         be any objects for comparing against nodes in the store, for
  334.         example, RegExLiteral, Date? DateRange?
  335.  
  336.         quoted table:                <id>_quoted_statements
  337.         asserted rdf:type table:     <id>_type_statements
  338.         asserted non rdf:type table: <id>_asserted_statements
  339.  
  340.         triple columns: subject,predicate,object,context,termComb,objLanguage,objDatatype
  341.         class membership columns: member,klass,context termComb
  342.  
  343.         FIXME:  These union all selects *may* be further optimized by joins
  344.  
  345.         '''
  346.         (subject, predicate, obj) = .1
  347.         quoted_table = '%s_quoted_statements' % self._internedId
  348.         asserted_table = '%s_asserted_statements' % self._internedId
  349.         asserted_type_table = '%s_type_statements' % self._internedId
  350.         literal_table = '%s_literal_statements' % self._internedId
  351.         c = self._db.cursor()
  352.         parameters = []
  353.         if predicate == RDF.type:
  354.             (clauseString, params) = self.buildClause('typeTable', subject, RDF.type, obj, context, True)
  355.             parameters.extend(params)
  356.             selects = [
  357.                 (asserted_type_table, 'typeTable', clauseString, ASSERTED_TYPE_PARTITION)]
  358.         elif isinstance(predicate, REGEXTerm) or predicate.compiledExpr.match(RDF.type) or not predicate:
  359.             selects = []
  360.             if (not (self.STRONGLY_TYPED_TERMS) and isinstance(obj, Literal) and not obj or self.STRONGLY_TYPED_TERMS) and isinstance(obj, REGEXTerm):
  361.                 (clauseString, params) = self.buildClause('literal', subject, predicate, obj, context)
  362.                 parameters.extend(params)
  363.                 selects.append((literal_table, 'literal', clauseString, ASSERTED_LITERAL_PARTITION))
  364.             
  365.             if not isinstance(obj, Literal):
  366.                 if isinstance(obj, REGEXTerm):
  367.                     pass
  368.                 if not (self.STRONGLY_TYPED_TERMS) or not obj:
  369.                     (clauseString, params) = self.buildClause('asserted', subject, predicate, obj, context)
  370.                     parameters.extend(params)
  371.                     selects.append((asserted_table, 'asserted', clauseString, ASSERTED_NON_TYPE_PARTITION))
  372.                 
  373.             (clauseString, params) = self.buildClause('typeTable', subject, RDF.type, obj, context, True)
  374.             parameters.extend(params)
  375.             selects.append((asserted_type_table, 'typeTable', clauseString, ASSERTED_TYPE_PARTITION))
  376.         elif predicate:
  377.             selects = []
  378.             if (not (self.STRONGLY_TYPED_TERMS) and isinstance(obj, Literal) and not obj or self.STRONGLY_TYPED_TERMS) and isinstance(obj, REGEXTerm):
  379.                 (clauseString, params) = self.buildClause('literal', subject, predicate, obj, context)
  380.                 parameters.extend(params)
  381.                 selects.append((literal_table, 'literal', clauseString, ASSERTED_LITERAL_PARTITION))
  382.             
  383.             if not isinstance(obj, Literal):
  384.                 if isinstance(obj, REGEXTerm):
  385.                     pass
  386.                 if not (self.STRONGLY_TYPED_TERMS) or not obj:
  387.                     (clauseString, params) = self.buildClause('asserted', subject, predicate, obj, context)
  388.                     parameters.extend(params)
  389.                     selects.append((asserted_table, 'asserted', clauseString, ASSERTED_NON_TYPE_PARTITION))
  390.                 
  391.             
  392.         if context is not None:
  393.             (clauseString, params) = self.buildClause('quoted', subject, predicate, obj, context)
  394.             parameters.extend(params)
  395.             selects.append((quoted_table, 'quoted', clauseString, QUOTED_PARTITION))
  396.         
  397.         q = self._normalizeSQLCmd(unionSELECT(selects, selectType = TRIPLE_SELECT_NO_ORDER))
  398.         self.executeSQL(c, q, parameters)
  399.         tripleCoverage = { }
  400.         result = c.fetchall()
  401.         c.close()
  402.         for rt in result:
  403.             (graphKlass, idKlass, graphId) = (s, p, o)
  404.             contexts = tripleCoverage.get((s, p, o), [])
  405.             contexts.append(graphKlass(self, idKlass(graphId)))
  406.             tripleCoverage[(s, p, o)] = contexts
  407.         
  408.         for s, p, o in tripleCoverage.items():
  409.             contexts = None
  410.             yield ((s, p, o), (lambda .0: for c in .0:
  411. c)(contexts))
  412.             extractTriple(rt, self, context)
  413.         
  414.  
  415.  
  416. CREATE_ASSERTED_STATEMENTS_TABLE = '\nCREATE TABLE %s_asserted_statements (\n    subject       text not NULL,\n    predicate     text not NULL,\n    object        text not NULL,\n    context       text not NULL,\n    termComb      tinyint unsigned not NULL)'
  417. CREATE_ASSERTED_TYPE_STATEMENTS_TABLE = '\nCREATE TABLE %s_type_statements (\n    member        text not NULL,\n    klass         text not NULL,\n    context       text not NULL,\n    termComb      tinyint unsigned not NULL)'
  418. CREATE_LITERAL_STATEMENTS_TABLE = '\nCREATE TABLE %s_literal_statements (\n    subject       text not NULL,\n    predicate     text not NULL,\n    object        text,\n    context       text not NULL,\n    termComb      tinyint unsigned not NULL,\n    objLanguage   varchar(3),\n    objDatatype   text)'
  419. CREATE_QUOTED_STATEMENTS_TABLE = '\nCREATE TABLE %s_quoted_statements (\n    subject       text not NULL,\n    predicate     text not NULL,\n    object        text,\n    context       text not NULL,\n    termComb      tinyint unsigned not NULL,\n    objLanguage   varchar(3),\n    objDatatype   text)'
  420. CREATE_NS_BINDS_TABLE = '\nCREATE TABLE %s_namespace_binds (\n    prefix        varchar(20) UNIQUE not NULL,\n    uri           text,\n    PRIMARY KEY (prefix))'
  421.